package com.bbx.taxi.client.widget.Dailog;

import java.util.ArrayList;
import java.util.List;

import com.bbx.androidapi.util.DensityUtil;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Coupons;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.Wallet.CouponActivity;
import com.bbx.taxi.client.Util.CouponUtils;
import com.bbx.taxi.client.Util.IsActivityFinishUtils;
import com.bbx.taxi.client.Util.TextViewAdaption;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/** 
 * @Path com.bbx.taxi.client.widget.Dailog.MyEnterCouponDailog.java
 * @Author 严旭文
 * @Date 2016-5-6 上午9:48:31
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 入账优惠劵
 */
public class MyEnterCouponDailog extends Dialog implements
		android.view.View.OnClickListener {
	public RelativeLayout layout_dailog;
	private Button  btn_confirm;
	private ImageView btn_exit;
	private ListView listview;
	private TextView tv_enter_coupon;
	private MyAdapter mMyAdapter;
	private List<Coupons> list;
	private boolean cancelable = true;
    private Context context;
    private boolean isFinish=false;
	public MyEnterCouponDailog(Context context) {
		super(context,(MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0)==0?R.style.Dialog_bocop:R.style.Dialog_bocop2);
		this.context=context;
		init();
	}

	private void init() {
		View contentView = View.inflate(getContext(), R.layout.dailog_entercoupon,
				null);
		setContentView(contentView);
		btn_exit = (ImageView) findViewById(R.id.btn_exit);
		btn_confirm = (Button) findViewById(R.id.btn_confirm);
		layout_dailog = (RelativeLayout) findViewById(R.id.layout_dailog);
		listview=(ListView)findViewById(R.id.listview);
		tv_enter_coupon=(TextView)findViewById(R.id.tv_enter_coupon);
		btn_exit.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);
		layout_dailog.setVisibility(View.GONE);
		list=new ArrayList<Coupons>();
		mMyAdapter=new MyAdapter(context, list);
		listview.setAdapter(mMyAdapter);
		this.setCancelable(false);
		contentView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelable) {
					dismiss();
				}
			}
		});
		
	}

	@Override
	public void show() {
		if(!IsActivityFinishUtils.isActivityFinish(context)){
		list.clear();
		if(MessageDialog.map_dialog!=null){
			int size=MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0;
		MessageDialog.map_dialog.put(context.toString(), size+1);
		if(MessageDialog.map_dialog.get(context.toString())!=null&&MessageDialog.map_dialog.get(context.toString())<=MessageDialog.max_dialog){
			super.show();
			Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_in);
			layout_dailog.startAnimation(a);
			layout_dailog.setVisibility(View.VISIBLE);
			isFinish=false;
		}else{
			MessageDialog.map_dialog.put(context.toString(), MessageDialog.map_dialog.get(context.toString())-1);
		}
		}
		}
	}

	@SuppressLint("NewApi")
	@Override
	public void dismiss() {
		if(!IsActivityFinishUtils.isActivityFinish(context)){
		if(isFinish)
		super.dismiss();
		int size=MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0;
		if(size>0){
			MessageDialog.map_dialog.put(context.toString(), size-1);
		}
		Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_out);
		layout_dailog.startAnimation(a);
		a.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation anim) {}
			
			@Override
			public void onAnimationRepeat(Animation anim) {}	
			@Override
			public void onAnimationEnd(Animation anim) {
				isFinish=true;
				dismiss();
			}
		});
		}

	}

	@Override
	public void setCancelable(boolean flag) {
		cancelable = flag;
		super.setCancelable(flag);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_exit:
			dismiss();
			break;
		case R.id.btn_confirm:
			dismiss();
			context.startActivity(new Intent(context,CouponActivity.class));
			break;
		}

	}
	public void setList(List<Coupons> list){
		this.list.addAll(list);
		setHeight(this.list.size());
		mMyAdapter.notifyDataSetChanged();
		tv_enter_coupon.setText(Html.fromHtml(String.format(context.getString(R.string.enter_coupon),"<font color=\"#fe5000\">"+this.list.size()+"</font>")));

	}
	public class MyAdapter extends BaseAdapter{
		List<Coupons> list;
		Context context;
		public MyAdapter(Context context,List<Coupons> list) {
			this.context=context;
			this.list=list;
		}
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder viewholder;
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(context);
				convertView = (View) inflater.inflate(R.layout.item_entercoupon, null);
				viewholder = new ViewHolder();
				viewholder.tv_money = (TextView) convertView
						.findViewById(R.id.tv_money);
				viewholder.tv_coupon_type = (TextView) convertView
						.findViewById(R.id.tv_coupon_type);
				viewholder.tv_unit = (TextView) convertView
						.findViewById(R.id.tv_unit);
				
				convertView.setTag(viewholder);
			} else
				viewholder = (ViewHolder) convertView.getTag();
			viewholder.tv_money.setText(list.get(position).getSum()+"");
			CouponUtils.setCouponType(context,viewholder.tv_coupon_type,viewholder.tv_money,viewholder.tv_unit,list.get(position).getType());
			TextViewAdaption.onAdaption(context, viewholder.tv_money, DensityUtil.dip2px(context, 60), DensityUtil.dip2px(context, 40));
			return convertView;
		}
		public class ViewHolder {
			TextView tv_money,tv_coupon_type,tv_unit;
		}
	}
	public void setHeight(int size){
		listview.setVisibility(View.GONE);
		//获取列表项的大小
				LayoutInflater inflater = LayoutInflater.from(context);
				View item_view = (View) inflater.inflate(R.layout.item_entercoupon, null);
				//外层布局要LinearLayout
				item_view.measure(0,
						MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			   int h = item_view.getMeasuredHeight()*((size==1)?1:2);
			   Log.e("xxx",h+"");
			   int w = item_view.getMeasuredWidth();
				//设置ListView的大小
				LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) listview.getLayoutParams();
				linearParams.height=h;
				linearParams.width=w;
				listview.setLayoutParams(linearParams);
				listview.setVisibility(View.VISIBLE);
	}
}
